﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.ServiceModel.Security;
using System.IdentityModel.Selectors;
using PSE.Framework.Common.Security.Services.Authentication.ClientCredentials;
using System.IdentityModel.Tokens;
using System.ServiceModel.Description;
using System.ServiceModel.Security.Tokens;

//TODO: Transformar em provider
namespace PSE.Framework.Service.Security.Authentication.ServiceCredentials
{
    internal class CustomServiceCredentialsSecurityTokenManager :
        ServiceCredentialsSecurityTokenManager
    {
        CustomServiceCredentials credentials;

        public CustomServiceCredentialsSecurityTokenManager(CustomServiceCredentials credentials)
            : base(credentials)
        {
            this.credentials = credentials;
        }

        public override SecurityTokenAuthenticator CreateSecurityTokenAuthenticator(SecurityTokenRequirement tokenRequirement, out SecurityTokenResolver outOfBandTokenResolver)
        {
            //TODO: Alterar caso passem a existir novas formas de autenticacao suportadas
            if (tokenRequirement.TokenType == SecurityTokenTypes.UserName)
            {
                //TODO: Duvida - Ta certo isso?
                MessageDirection direction = tokenRequirement.GetProperty<MessageDirection>(ServiceModelSecurityTokenRequirement.MessageDirectionProperty);
                if (direction == MessageDirection.Input)
                {
                    outOfBandTokenResolver = null;
                    return new UserNameTokenAuthenticator();
                }
                else
                {
                    return base.CreateSecurityTokenAuthenticator(tokenRequirement, out outOfBandTokenResolver);
                }
            }

            return base.CreateSecurityTokenAuthenticator(tokenRequirement, out outOfBandTokenResolver);

        }

        public override SecurityTokenSerializer CreateSecurityTokenSerializer(SecurityTokenVersion version)
        {
            return new WSSecurityTokenSerializer();
        }
    }

}
